package com.larry.mapreduce.mapjoin;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @ProjectName MapReduceDemo
 * @Author larry
 * @Date 20:41 2021/11/9
 * @Description TODO
 **/
public class MapJoinDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
        //1 获取job
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        //2.设置jar路径
        job.setJarByClass(MapJoinDriver.class);
        //3.关联mapper和reducer
        job.setMapperClass(MapJoinMapper.class);
        //job.setReducerClass(TableReducer.class);
        //4.设置map输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        //5.设置最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //加载缓存数据
        job.addCacheFile(new URI("file:///C:/Users/larry/IdeaProjects/MapReduceDemo/src/main/resources/tablecache/pd.txt"));

        //设置reduceTask为0则表示不需要reduce阶段处理
        job.setNumReduceTasks(0);

        //6.设置输入路径和输出路径
        FileInputFormat.setInputPaths(job,new Path("C:\\Users\\larry\\IdeaProjects\\MapReduceDemo\\src\\main\\resources\\inputtable2\\order.txt"));
        FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\larry\\IdeaProjects\\MapReduceDemo\\src\\main\\resources\\inputtable2\\out1"));
        //7.提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
